//
//  TIMInviteViewController.m
//  Demo_sdk
//
//  Created by Deli Xia on 2018/8/17.
//  Copyright © 2018年 Deli Xia. All rights reserved.
//

#import "TIMInviteViewController.h"
#import "Masonry.h"
#import "ProgressHUD.h"

#import "TIMStorageModel.h"
#import "TIMStorageManager.h"
#import "TIMApi.h"

@interface TIMInviteViewController ()

@property (nonatomic, strong) UIView *inviterView;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UIView *inviteedView;

@end

@implementation TIMInviteViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"邀请详情";
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.inviterView];
    [self.view addSubview:self.lineView];
    [self.view addSubview:self.inviteedView];
    
    [self setupConstraint];
}

- (void)setupConstraint {
    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height/2 - 60)];
    TIMUserInfo *inviteUser = [[TIMStorageManager sharedStorageManager] getLocalContactByFriendID:self.groupInfo.inviterID];
    label1.numberOfLines = 0;
    label1.text = [NSString stringWithFormat:@"InvitedUserId:%@,avatar:%@, 邀请%ld位朋友加入群聊 邀请说明：%@",self.groupInfo.inviterID,inviteUser.avatarURL,self.groupInfo.inviteCount,self.groupInfo.inviteRemarks];
    [self.inviterView addSubview:label1];
    
    UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.bounds.size.height/2 - 100)];
    label1.numberOfLines = 0;
    // 找到ID后，可以直接从数据库里查询头像，当前demo没做
    NSMutableString *inviteString = [NSMutableString new];
    for (NSString *inviteed in self.groupInfo.invitedIDs) {
        [inviteString appendString:inviteed];
    }
    NSMutableString *acceptString = [NSMutableString new];
    for (NSString *accept in self.groupInfo.acceptedIDs) {
        [acceptString appendString:accept];
    }
    label2.text = [NSString stringWithFormat:@"Inviteds:%@,acceptIds:%@",inviteString,acceptString];
    [self.inviteedView addSubview:label2];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(self.view.center.x - 50, self.view.bounds.size.height/2 - 100 , 100, 40);
    button.backgroundColor = UIColor.blueColor;
    [button setTitle:@"确认邀请" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(clickConfirm) forControlEvents:UIControlEventTouchUpInside];
    [self.inviteedView addSubview:button];
}

#pragma mark - action

- (void)clickConfirm {
    
    
    [TIMApi inviteOperationWithGroup:self.groupInfo.groupID inviteRemarks:nil inviteAuditAck:self.groupInfo.inviteAuditAckKey MsgOperation:TIMOperateGroupTypeGrpOfferAuditAck groupMembers:self.groupInfo.invitedIDs completion:^(NSError *error) {
        if (!error) {
            NSLog(@"---------邀请确认成功----------------------");
            dispatch_async(dispatch_get_main_queue(), ^{
                 [self.navigationController popViewControllerAnimated:YES];
            });
           
        } else {
            [ProgressHUD showHudTipStr:[NSString stringWithFormat:@"邀请失败,%ld",error.code]];
            NSLog(@"错误码%ld",(long)error.code);
        }
    }];
}

#pragma mark - Getter

- (UIView *)inviterView {
    if (!_inviterView) {
        _inviterView = [[UIView alloc] initWithFrame:CGRectMake(0, 60, self.view.frame.size.width, self.view.frame.size.height / 2 - 60 - 5)];
    }
    return _inviterView;
}

- (UIView *)lineView {
    if (!_lineView) {
        _lineView = [[UIView alloc] initWithFrame:CGRectMake(10, self.view.frame.size.height / 2 - 1, self.view.frame.size.width - 20, 2)];
        _lineView.backgroundColor = UIColor.lightGrayColor;
    }
    return _lineView;
}

- (UIView *)inviteedView {
    if (!_inviteedView) {
        _inviteedView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height / 2 + 1, self.view.frame.size.width,  self.view.frame.size.height / 2 - 2)];
    }
    return _inviteedView;
}

@end
